/**
 * Created by WYH on 17/4/14.
 */
angular.module('navBar', [])
    .component('navBar', {
        templateUrl: 'component/base/navBar/nav_bar.template.html',
        controller: ['$rootScope', '$scope', 'Api', 'Enum', '$state', 'CompanyModel', function ($rootScope, $scope, Api, Enum, $state, CompanyModel) {


            setupNavSelectedWithState($state.current.name);

            $scope.barItems = Enum.NavBarNormalItems;

            setupNavSelectWhenStateChange();

            getCompanyDetail();

            $scope.bindSignOutModule = bindSignOutModule;

            $scope.gotoAccountManager = function () {
                $state.go('account');
            };

            function bindSignOutModule() {
                window.localStorage.clear();

                window.location.href = '/tribalc-web/login.html';

            }


            function getCompanyDetail() {
                if(!Api.getAssign()){
                    return false;
                }
                Api.getCompanyDetail(Api.getAssign(), function (result) {
                    console.log(result)
                    var company = new CompanyModel(result.data);
                    $rootScope.RootCompanyInfo = company;
                    $rootScope.companyName = result.data.companyName

                    $scope.companyName = company.companyName;
                    $scope.companyLogo = company.logo;


                    // angular.module('tribalc').constant('CompanyInfo', result.data);

                    if (result.data.propertyNature) {
                        $scope.barItems = Enum.NavBarPropertyItems;
                    }
                });
            }


            function setupNavSelectWhenStateChange() {
                /* 获取路由变化 */
                $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
                    setupNavSelectedWithState(toState.name);
                });
            }


            function setupNavSelectedWithState(stateName) {
                if (stateName.indexOf('company.company') != -1) {
                    $scope.navSelect = Enum.NavBarPropertyItems[0];

                }else if (stateName.indexOf('company.approve') != -1) {
                    $scope.navSelect = Enum.NavBarPropertyItems[3];
                } else if (stateName.indexOf('company.managerLock') != -1) {
                    $scope.navSelect = Enum.NavBarPropertyItems[2];
                } else if (stateName.indexOf('company.managerRent') != -1) {
                    $scope.navSelect = Enum.NavBarPropertyItems[1];
                }  else if (stateName === 'account') {
                    $scope.navSelect = null;

                }
            }


        }]
    });